<?php
declare (strict_types=1);

namespace app\admin\validate;

use app\common\model\Admin as AdminModel;
use think\Validate;

class Admin extends Validate
{
    protected $rule = [
        'admin_name' => 'require|min:6|alphaNum|checkOnly:admin_name',
        'mobile' => 'require|mobile',
        'role_id' => 'require'
    ];

    protected $message = [
        'admin_name.require' => '请填写用户名',
        'admin_name.min' => '用户名至少六位',
        'admin_name.alphaNum' => '用户名只能为数字和字母',
        'admin_name.checkOnly' => '用户名已存在',
        'mobile.require' => '请填写手机号',
        'mobile.mobile' => '手机号格式有误',
        'role_id.require' => '请选择角色'
    ];

    protected function checkOnly($value, $rule, $data = []): bool
    {
        $that = AdminModel::where([$rule => $value]);
        if (!empty($data['id'])) {
            $that->where('id', '<>', $data['id']);
        }
        $count = $that->count();

        return $count ? false : true;
    }
}